package com.apps.ftapp.act;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.activity.BaseActivity;
import com.apps.ftapp.model.BankModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;

/**
 * 提现
 */
public class TiXianActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.bank_name)
    TextView bankName;
    @BindView(R.id.bank_num)
    TextView bankNum;
    @BindView(R.id.more)
    TextView more;
    @BindView(R.id.bank_btn)
    RelativeLayout bankBtn;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.shouxufei)
    TextView shouxufei;
    @BindView(R.id.edit_money)
    EditText editMoney;
    @BindView(R.id.yongjin)
    TextView yongjin;
    @BindView(R.id.tixian)
    TextView tixian;
    @BindView(R.id.tijiao)
    Button tijiao;
    @BindView(R.id.activity_tx)
    LinearLayout activityTx;

    private String account_id;

//    private


    private Gson gson = new Gson();

    private String curmoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ti_xian);
        ButterKnife.bind(this);
        setTitle("提现");

        loadData();

        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        curmoney = bundle.getString("money");

        yongjin.setText("当前佣金" + curmoney + "元");


        tixian.setOnClickListener(this);
        tijiao.setOnClickListener(this);
        bankBtn.setOnClickListener(this);

    }

    private void loadData() {

        RequestParams params = new RequestParams();
        params.put("userId", Constant.uid);
        params.put("token", Constant.token);
        Async.post("course/share/account/info", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody) + "......");
                    BankModel result = gson.fromJson(new String(responseBody), BankModel.class);
                    if (Tools.isIntNull(result.getSuccess()) == 1) {

                        account_id = result.getAccountinfo().getAccount_id();
//                        bankName.setText(result.getAccountinfo().getName());
//                        bankNum.setText(result.getAccountinfo().getMobile());
                        bankName.setText(result.getAccountinfo().getBankname());
                        bankNum.setText(result.getAccountinfo().getBankaccount());
                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tixian:

                editMoney.setText(curmoney);

                break;

            case R.id.tijiao:

                tixian();

                break;

            case R.id.bank_btn:
                //提交设置
//                Intent intent = new Intent(TxActivity.this, BankActivity.class);
//                startActivity(intent);

                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("isFlag", "1");
                ActivitySkipUtil.skipAnotherActivity(TiXianActivity.this, BankActivity.class, false);

                break;
        }
    }

    private void tixian() {


        String edit = editMoney.getText().toString();
        if ("".equals(edit) || "0".equals(edit)) {
            Toasty.normal(getApplicationContext(), "请输入正确的提现金额").show();
            return;
        }

        if (null == account_id || "".equals(Tools.isNull(account_id))) {
            Toasty.normal(getApplicationContext(), "请先设置提现账户").show();
            return;
        }

        L.i("main", "account_id:" + account_id + "::" + Constant.uid + "::" + Constant.token + "::" + edit);

        RequestParams params = new RequestParams();
        params.put("userId", Constant.uid);
        params.put("token", Constant.token);
        params.put("account_id", account_id);
        params.put("money", edit);
        Async.post("course/share/account/withdrawal", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody) + "......");
                    BankModel result = gson.fromJson(new String(responseBody), BankModel.class);
                    if (Tools.isIntNull(result.getSuccess()) == 1) {
                        Toasty.normal(getApplicationContext(), "提交申请成功，等待管理员审核").show();

                    } else {

                        Toasty.normal(getApplicationContext(), result.getError()).show();
                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }

}
